import random

from django.shortcuts import render
from django.utils import timezone
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse, QueryDict
from InfoManage.models import *
from InfoManage.serializers import *

# Create your views here.
from LoginManage.models import Staff
from PersonManage.models import Driver


@csrf_exempt
def getComplains(request):
    if request.method == 'GET':
        print('GET')
        ManagerID = request.GET.get('id')
        Status = request.GET.get('Status')
        if Status == '未回复':
            info_list = Information.objects.filter(ManagerID=ManagerID, Status='未回复')
            infos = []
            for info in info_list:
                infos.append(info.to_dict())
            return JsonResponse({"response": infos}, safe=False)
        elif Status == '已回复':
            info_list = Information.objects.filter(ManagerID=ManagerID, Status='已回复')
            infos = []
            for info in info_list:
                infos.append(info.to_dict())
            return JsonResponse({"response": infos}, safe=False)
        elif Status == '已处理':
            info_list = Information.objects.filter(ManagerID=ManagerID, Status='已处理')
            infos = []
            for info in info_list:
                infos.append(info.to_dict())
            return JsonResponse({"response": infos}, safe=False)


@csrf_exempt
def checkReply(request):
    if request.method == 'POST':
        ComplainedLine = request.POST.get('ComplainedLine')
        ComplainedDriverID = request.POST.get('ComplainedDriverID')
        ComplainedTime = request.POST.get('ComplainedTime')
        CustomerID = request.POST.get('CustomerID')
        info = Information.objects.get(ComplainedLine=ComplainedLine, ComplainedDriverID=ComplainedDriverID,
                                       ComplainedTime=ComplainedTime, CustomerID=CustomerID)
    return JsonResponse({"response": info.to_dict1()}, safe=False)


@csrf_exempt
def replyComplain(request):
    if request.method == 'POST':
        print(request.POST.get)
        ComplainedLine = request.POST.get('ComplainedLine')
        ComplainedDriverID = request.POST.get('ComplainedDriverID')
        ComplainedTime = request.POST.get('ComplainedTime')
        CustomerID = request.POST.get('CustomerID')
        Status = request.POST.get('Status')
        try:
            info = Information.objects.get(ComplainedLine=ComplainedLine, ComplainedDriverID=ComplainedDriverID,
                                           ComplainedTime=ComplainedTime, CustomerID=CustomerID)
        except Information.DoesNotExist:
            return JsonResponse({"Message": "this message is not existed"}, safe=False)
        if info.Status == '已回复':
            return JsonResponse({"Message": "this message is already replayed"}, safe=False)
        else:
            Content = request.POST.get('Content')
            info.Content = Content
            info.Status = Status
            info.save()
            return JsonResponse({"Message": "replay success"}, safe=False)


@csrf_exempt
def handleComplain(request):
    if request.method == 'POST':
        print(request.POST.get)
        ComplainedLine = request.POST.get('ComplainedLine')
        ComplainedDriverID = request.POST.get('ComplainedDriverID')
        ComplainedTime = request.POST.get('ComplainedTime')
        CustomerID = request.POST.get('CustomerID')
        Status = request.POST.get('Status')
        try:
            info = Information.objects.get(ComplainedLine=ComplainedLine, ComplainedDriverID=ComplainedDriverID,
                                           ComplainedTime=ComplainedTime, CustomerID=CustomerID)
        except Information.DoesNotExist:
            return JsonResponse({"Message": "this message is not existed"}, safe=False)
        if info.Status == '已处理':
            return JsonResponse({"Message": "this message is already handled"}, safe=False)
        else:
            info.Status = Status
            info.save()
            return JsonResponse({"Message": "handle success"}, safe=False)


@csrf_exempt
def deleteComplain(request):
    if request.method == 'POST':
        print(request.POST.get)
        CustomerID = request.POST.get('CustomerID')
        ComplainedLine = request.POST.get('ComplainedLine')
        ComplainedTime = request.POST.get('ComplainedTime')
        ComplainedDriverID = request.POST.get('ComplainedDriverID')
        try:
            info = Information.objects.get(ComplainedLine=ComplainedLine, ComplainedDriverID=ComplainedDriverID,
                                           ComplainedTime=ComplainedTime, CustomerID=CustomerID)
        except Information.DoesNotExist:
            return JsonResponse({"Message": "this message is not existed"}, safe=False)
        info.delete()
        return JsonResponse({"Message": "delete information success"}, safe=False)


# @csrf_exempt
# def addComplain(request):  # 判断司机存在？路线存在？司机和路线对应？
#     if request.method == 'POST':
#         print(request.POST.get)
#         ComplainedLine = request.POST.get('ComplainedLine')
#         ComplainedDriverID = request.POST.get('ComplainedDriverID')
#         ComplainedTime = request.POST.get('ComplainedTime')
#         CustomerID = request.POST.get('CustomerID')
#         try:
#             driver = Driver.objects.get(id=ComplainedDriverID)
#         except Driver.DoesNotExist:
#             return JsonResponse({"Message": "driver id is not existed"}, safe=False)
#         info = Information.objects.filter(ComplainedLine=ComplainedLine, ComplainedDriverID=ComplainedDriverID,
#                                           ComplainedTime=ComplainedTime, CustomerID=CustomerID)
#         if info.exists():
#             return JsonResponse({"Message": "this message is existed"}, safe=False)
#         else:
#             newInfo = InformationSerializer(data=request.POST)
#             if newInfo.is_valid():
#                 manager_list = random.sample(list(Staff.objects.filter(id__startswith="CS").values('id')), 1)
#                 print(manager_list[0].get('id'))
#                 newInfo.save()
#                 newInfo1 = Information.objects.get(ComplainedLine=ComplainedLine,
#                                                    ComplainedDriverID=ComplainedDriverID,
#                                                    ComplainedTime=ComplainedTime, CustomerID=CustomerID)
#                 newInfo1.ManagerID = manager_list[0].get('id')
#                 newInfo1.Status = '未回复'
#                 newInfo1.save()
#             else:
#                 print(newInfo.errors)
#                 return JsonResponse({"Message": "Arguments Illegal"}, safe=False)
#             return JsonResponse({"Message": "Add new complain Success"}, safe=False)


@csrf_exempt
def addComplain(request):
    if request.method == 'POST':
        print(request.POST.get)
        ComplainedLine = request.POST.get('ComplainedLine')
        ComplainedDriverID = request.POST.get('ComplainedDriverID')
        ComplainedTime = request.POST.get('ComplainedTime')
        CustomerID = request.POST.get('CustomerID')
        CarSendTime = request.POST.get('CarSendTime')
        CarSendDate = request.POST.get('CarSendDate')
        Car = request.POST.get('Car')
        try:
            driver = Driver.objects.get(id=ComplainedDriverID)
        except Driver.DoesNotExist:
            return JsonResponse({"Message": "driver id is not existed"}, safe=False)
        info = Information.objects.filter(ComplainedLine=ComplainedLine, ComplainedDriverID=ComplainedDriverID,
                                          ComplainedTime=ComplainedTime, CustomerID=CustomerID)
        if info.exists():
            return JsonResponse({"Message": "this message is existed"}, safe=False)
        else:
            newInfo = InformationSerializer(data=request.POST)
            if newInfo.is_valid():
                manager_list = random.sample(list(Staff.objects.filter(id__startswith="CS").values('id')), 1)
                print(manager_list[0].get('id'))
                newInfo.save()
                newInfo1 = Information.objects.get(ComplainedLine=ComplainedLine,
                                                   ComplainedDriverID=ComplainedDriverID,
                                                   ComplainedTime=ComplainedTime, CustomerID=CustomerID)
                newInfo1.ManagerID = manager_list[0].get('id')
                newInfo1.CarSendTime = CarSendTime
                newInfo1.CarSendDate = CarSendDate
                newInfo1.Car = Car
                newInfo1.Status = '未回复'
                newInfo1.save()
            else:
                print(newInfo.errors)
                return JsonResponse({"Message": "Arguments Illegal"}, safe=False)
            return JsonResponse({"Message": "Add new complain Success"}, safe=False)



@csrf_exempt
def UsercheckReply(request):
    if request.method == 'POST':
        print(request.POST.get)
        ComplainedLine = request.POST.get('ComplainedLine')
        ComplainedDriverID = request.POST.get('ComplainedDriverID')
        CustomerID = request.POST.get('CustomerID')
        ComplainedTime = request.POST.get('ComplainedTime')
        info = Information.objects.get(ComplainedLine=ComplainedLine, ComplainedDriverID=ComplainedDriverID,
                                       ComplainedTime=ComplainedTime, CustomerID=CustomerID)

    return JsonResponse({"response": info.to_dict5()}, safe=False)


def getAllComplain(request):
    if request.method == 'GET':
        print('GET')
        id = request.GET.get('id')
        complain_list = Information.objects.filter(CustomerID=id)
        complains = []
        for complain in complain_list:
            complains.append(complain.to_dict6())
        return JsonResponse({"complains": complains}, safe=False)
